package com.manlost.upload.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.manlost.biz.ManlostServiceLocator;
import com.manlost.biz.baby.BabyService;
import com.manlost.biz.baby.model.dataobject.Baby;
import com.manlost.biz.common.CnToSpell;
import com.manlost.upload.AblumUploadService;
import com.manlost.upload.model.constant.AblumConstant;
import com.manlost.upload.model.constant.FTPConstant;
import com.manlost.upload.model.dataobject.AblumFolder;
import com.manlost.upload.model.dataobject.LocalBabyInfo;
import com.manlost.upload.operate.FTPOp;
import com.manlost.upload.util.AblumFolderUtil;

/**
 * 类AblumUploadServiceImpl.java的实现描述：TODO 类实现描述
 * 
 * @author xiaochuan.luxc 2012-5-6 下午1:12:33
 */
@Service
public class AblumUploadServiceImpl implements AblumUploadService {

    private BabyService babyService = ManlostServiceLocator.getBabyService();

    public void uploadLocalAblum(String ablumPath) {
        if (StringUtils.isBlank(ablumPath)) {
            return;
        }
        File ablum = new File(ablumPath);
        if (!ablum.exists() || !ablum.isDirectory()) {
            return;
        }
        AblumFolder ablumFolder = AblumFolderUtil.generateAblumFolder(ablumPath);
        FTPOp ftpOp = new FTPOp();
        try {
            ftpOp.connectDefaultServer();
            ftpOp.mkdir(CnToSpell.getValidName(ablumFolder.getYear()));
            ftpOp.cd(CnToSpell.getValidName(ablumFolder.getYear()));
            ftpOp.mkdir(CnToSpell.getValidName(ablumFolder.getMonDay()));
            ftpOp.cd(CnToSpell.getValidName(ablumFolder.getMonDay()));
            ftpOp.mkdir(CnToSpell.getValidName(ablumFolder.getSource()));
            ftpOp.cd(CnToSpell.getValidName(ablumFolder.getSource()));
            ftpOp.mkdir(CnToSpell.getValidName(ablumFolder.getAuthor()));
            ftpOp.cd(CnToSpell.getValidName(ablumFolder.getAuthor()));
            ftpOp.mkdir(CnToSpell.getValidName(ablumFolder.getAlbum()));
            ftpOp.cd(CnToSpell.getValidName(ablumFolder.getAlbum()));

            for (LocalBabyInfo localBaby : ablumFolder.getLocalPicBabyInfos()) {
                ftpOp.upload(localBaby.getLocalAbsolutePath(), localBaby.getLocalFileName());
                Baby baby = new Baby();
                baby.setBabyType(localBaby.getBabyType());
                baby.setBabyAlbum(ablumFolder.getAlbum());
                baby.setBabyMonthDate(ablumFolder.getMonDay());
                baby.setBabySource(ablumFolder.getSource());
                baby.setBabyYear(ablumFolder.getYear());
                baby.setImageRaw(localBaby.getLocalFileName());
                babyService.insetOneBaby(baby);
            }
            FileWriter f = new FileWriter(new File(ablumPath + "/" + AblumConstant.IS_UPLOAD_FILENAME));
            f.write("");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
